package _01_动态数组;

public class _35_搜索插入位置 {

    public static void main(String[] args) {

        _35_搜索插入位置 v = new _35_搜索插入位置();

        int[] ins = {1, 3, 5, 6};

        System.out.println(v.searchInsert(ins, 2));
    }

    public int searchInsert(int[] nums, int target) {
        int n = nums.length;
        int begin = 0;
        int end = n - 1;
        int result = n;
        while (begin <= end) {
            int mid = begin + end >> 1;
            // 找到第一个比他大的
            if (target <= nums[mid]) {
                result = mid;
                end = mid - 1;
            } else {
                begin = mid + 1;
            }
        }
        return result;
    }

}
